<?php
/**
 * Yii view auto
 *
 * @author <KTD SOFT>
 * @since Jan 19, 2013 - 10:54:11 AM
 */
$moduleDir = $_SERVER['DOCUMENT_ROOT'] . Yii::app()->baseUrl . '/protected/modules/';
$auth      = Yii::app()->authManager;
?>
<form method="POST" action="">    
    <div class="row-fluid">
        <!--Pagination Start-->
        <div class="grid">
            <div class="grid-title">
                <div class="pull-left"><?php echo $this->pageTitle ?></div>
                <div class="pull-right">
                    <button class="btn btn-primary" type="submit">
                        <?php echo Yii::t('form', 'Save') ?>
                    </button>
                </div>
                <div class="clear"></div>
            </div>

            <div class="grid-content">
                <table class="table table-striped table-mod-2">
                    <thead>
                        <tr>
                            <th><?php echo Yii::t('auth', 'Name') ?></th>
                            <th><?php echo Yii::t('auth', 'Description') ?></th>
                            <th><?php echo Yii::t('auth', 'Info') ?></th>
                        </tr>
                    </thead>
                    <tbody>

                        <?php if (is_dir($moduleDir)): ?>
                            <?php $modules = scandir($moduleDir); ?>
                            <?php foreach ($modules as $module): ?>
                                <?php
                                // Đưa các thư mục có tên là . và .. về rỗng để bỏ qua 
                                $module = trim($module, ".");
                                // Bỏ qua các thư mục có tên rỗng
                                if ($module == '')
                                    continue;
                                ?>
                                <?php $dir    = "$moduleDir/$module"; ?>
                                <?php if (is_dir($dir)): ?>
                                    <?php $controllers = scandir("$dir/controllers"); ?>
                                    <?php foreach ($controllers as $ctrl): ?>
                                        <?php
                                        // Đưa các thư mục có tên là . và .. về rỗng để bỏ qua 
                                        $ctrl = trim($ctrl, ".");
                                        // Bỏ qua các thư mục có tên rỗng
                                        if ($ctrl == '')
                                            continue;

                                        // Kiểm tra file nếu không thuộc dạng AbcController.php thì bỏ qua
                                        if (strpos($ctrl, "Controller.php") === FALSE)
                                            continue;

                                        $ctrlName = str_replace("Controller.php", "", $ctrl);
                                        ?>
                                        <tr>
                                            <td>
                                                <input type="checkbox" name="auth[]" value="<?php echo "$ctrlName.*" ?>"
                                                <?php if ($auth->getAuthItem("$ctrlName.*")) echo 'disabled="disabled" checked="checked"' ?> 
                                                       id="<?php echo $ctrlName ?>" onclick="checkChild('<?php echo $ctrlName ?>')"/>
                                                <label for="<?php echo $ctrlName ?>">
                                                    <span></span>
                                                    <?php $tag      = $auth->getAuthItem("$ctrlName.*") ? "s" : 'strong'; ?>
                                                    <<?php echo $tag ?> class="label label-success">
                                                    <?php echo "$ctrlName.*" ?>
                                                    </<?php echo $tag ?>>
                                                </label>
                                            </td>
                                            <td><?php echo "Manage $ctrlName" ?></td>
                                            <td><i><?php echo "$dir/controllers/$ctrl" ?></i></td>
                                        </tr>
                                        <?php
                                        if ($ctrlName != 'Auth')
                                            include("$dir/controllers/$ctrl")
                                            ?>
                                        <?php $methods  = get_class_methods(ucfirst($ctrlName) . "Controller"); ?>
                                        <?php foreach ($methods as $method): ?>
                                            <?php
                                            // Bỏ qua các phương thước không thuộc dạng: "actionIndex"
                                            if (!preg_match("/^action[^s]+/i", $method))
                                                continue;

                                            // Cắt lấy phần tên của action
                                            $action = str_replace("action", "", $method);
                                            ?>
                                            <tr>
                                                <td>                                                
                                                    <input name="auth[]"  value="<?php echo "$ctrlName.$action" ?>"
                                                           type="checkbox" <?php if ($auth->getAuthItem("$ctrlName.$action")) echo 'disabled="disabled" checked="checked"' ?>
                                                           id="<?php echo "$ctrlName.$action" ?>" class="<?php echo $ctrlName ?>" />
                                                    <label for="<?php echo "$ctrlName.$action" ?>">
                                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                                        <span></span>
                                                        <?php $tag    = $auth->getAuthItem("$ctrlName.$action") ? "s" : 'strong'; ?>
                                                        <<?php echo $tag ?> class="label">
                                                        <?php echo "$ctrlName.$action" ?>
                                                        </<?php echo $tag ?>>                                                    
                                                    </label>
                                                </td>
                                                <td><?php echo "$action $ctrlName" ?></td>
                                                <td><i><?php echo $method ?></i></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</form>
<script>
    function checkChild(name)
    {
        if ($("#" + name).attr('checked') == 'checked')
            $("." + name).attr('checked', 'checked');
        else
            $("." + name).removeAttr('checked');
    }
</script>